<?php
/**
 * rss_parser.php
 * 
 * Contains functions which parse different RSS feeds, prints the parsed RSS feed into the format it will appear as on the website,
 * and parses a user's preferences,
 */

/**
 * Takes an array containing the relevant data from a parsed RSS feed and prints the data in the format it will appear as on the website. 
 * 
 * @param array $array, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function print_parsed_feed($array) {
	for ($i = 0; $i < 3; $i++) {
		$headline = $array[$i][0];
		$url = $array[$i][1];
		$description = $array[$i][2];
		$website = $array[$i][3];
		$genre = $array[$i][4];
		
		if ($i == 0) {
			echo <<<EOL
	<h2>$website Headlines</h2>
	<p><h2><a href="$url" target="_blank">$headline</a></h2></p>
	<p><h3>$description</h3></p>
	<p><iframe src="$url" height="500px" width="95%"></iframe></p>
	<hr/>
EOL;
		}
		else {
			echo <<<EOL
	<p><h3><a href="$url" target="_blank">$headline</a></h3></p>
	<p><h4>$description</h4></p>
	<hr/>
EOL;
		}
	}
}

/**
 * Parses the string of preferences into an array.  This array will be used for displaying website preferences.
 * 
 * @param string $string, the string of prefernces
 * @return array $prefArray, array of parsed preferences string
 */
function parse_preferences($string) {
	$prefArray = array();
	$stringLength = strlen($string);
	for ($i = 0; $i < $stringLength; $i++) {
		$prefArray[$i] = substr($string,$i,1);
	}
	return $prefArray;
}

/**
 * Parses 5 headlines and their URLs from the CNN News RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_cnn_news_feed() {
	$url = "http://rss.cnn.com/rss/cnn_topstories.rss";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 1600; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				$temp = "";
			}
			else if (substr($data, $i, 8) === "</title>") {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 26) === '<guid isPermaLink="false">') {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</guid>") {
				$link = substr($temp, 25, (strlen($temp) - 25));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>") {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 4) === "&lt;" && $descriptionFound == TRUE) {
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "CNN News";
				$headlines[$j][4] = "News";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Google News RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_google_news_feed() {
	$url = "http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 200; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 12) !== "<title>Top S") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 0));
				$headlines[$j][0] =	$headline;
				$headlines[$j][2] = $headline;
				$temp = "";
			}
			else if (substr($data, $i, 9) === "&amp;url=" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$link = substr($temp, 8, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$headlines[$j][3] = "Google News";
				$headlines[$j][4] = "News";
				$temp = "";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Huffington Post RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_huffington_feed() {
	$url = "http://www.huffingtonpost.com/feeds/verticals/world/index.xml";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 34) !== "<title>World on HuffingtonPost.com") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 45) === "<link rel=\"alternate\" type=\"text/html\" href=\"" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 3) === "\"/>" && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$link = substr($temp, 44, (strlen($temp) - 44));
				$headlines[$j][1] = $link;
				$temp = "";
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Huffington Post";
				$headlines[$j][4] = "News";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Yahoo! News RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_yahoo_news_feed() {
	$url = "http://news.yahoo.com/rss/";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 690; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 13) !== "<title>Yahoo!") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 8));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === 'href="' && $headlineFound == TRUE) {
				$temp = "";
				$linkFound = TRUE;
			}
			else if (substr($data, $i, 5) === '"&gt;' && $headlineFound == TRUE && $linkFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
				$linkFound = FALSE;
			}
			else if (substr($data, $i, 10) === "&lt;/a&gt;" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 10) === "&lt;/p&gt;" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 9, (strlen($temp)-10));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "Yahoo! News";
				$headlines[$j][4] = "News";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Ars Technica RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_ars_feed() {
	$url = "http://feeds.arstechnica.com/arstechnica/index?format=xml";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>Ars") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 22) === "<description><![CDATA[" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 17) === "]]></description>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "Ars Technica";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the CNET RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_cnet_feed() {
	$url = "http://feeds.feedburner.com/cnet/tcoc";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 1420; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 16) !== "<title>CNET News") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
				$linkFound = TRUE;
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE && $linkFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
				$linkFound = FALSE;
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 4) === "&lt;" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-4));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "CNET";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Engadget RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_engadget_feed() {
	$url = "http://www.engadget.com/rss.xml";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 610; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 11) !== "<title>Enga") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 15, (strlen($temp) - 18));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 3) === "<p>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 4) === "</p>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 3, (strlen($temp)-3));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "Engadget";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Gizmodo RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_gizmodo_feed() {
	$url = "http://feeds.gawker.com/Gizmodo/full";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>Giz") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 14) === "</description>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Gizmodo";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the IGN RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_ign_feed() {
	$url = "http://feeds.ign.com/ign/news";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 1100; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>IGN") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 14) === "</description>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "IGN";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Joystiq RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_joystiq_feed() {
	$url = "http://www.joystiq.com/rss.xml";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 16) === "<title><![CDATA[") {
				$temp = "";
				$headlineFound = TRUE;
			}
			else if (substr($data, $i, 11) === "]]></title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 15, (strlen($temp) - 15));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Joystiq";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Kotaku RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_kotaku_feed() {
	$url = "http://feeds.gawker.com/Kotaku/full";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>Giz") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 14) === "</description>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Kotaku";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Maximum PC RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_max_feed() {
	$url = "http://www.maximumpc.com/articles/all/feed";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>Max") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 14) === "</description>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = "";
//				$headlines[$j][2] = $description;
				$headlines[$j][3] = "Maximum PC";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Wired RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_wired_feed() {
	$url = "http://feeds.wired.com/wired/index?format=xml";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 800; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 10) !== "<title>Wir") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 26) === "<content:encoded><![CDATA[" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 18) === "</content:encoded>" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-12));
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Wired";
				$headlines[$j][4] = "Tech";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the CNN SI RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_cnn_si_feed() {
	$url = "http://rss.cnn.com/rss/si_topstories.rss";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 1600; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 13) !== "<title>SI.com") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
			}
			else if (substr($data, $i, 13) === "<description>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 4) === "&lt;" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 12, (strlen($temp)-13));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "CNN SI";
				$headlines[$j][4] = "Sports";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the ESPN RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_espn_feed() {
	$url = "http://sports.espn.go.com/espn/rss/news";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 250; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 15) !== "<title>ESPN.com" || substr($data, $i, 13) !== "<title>ESPN l") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 15, (strlen($temp) - 18));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 10) === "share?url=" && $headlineFound == TRUE) {
				$temp = "";
				$linkFound = TRUE;
			}
			else if (substr($data, $i, 5) === "&amp;" && $headlineFound == TRUE && $linkFound == TRUE) {
				$link = substr($temp, 9, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
				$linkFound = FALSE;
			}
			else if (substr($data, $i, 4) === "</a>" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 1) === "]" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 3, (strlen($temp)-4));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "ESPN";
				$headlines[$j][4] = "Sports";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Sporting News RSS Feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_sportingnews_feed() {
	$url = "http://aol.sportingnews.com/rss";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 0; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 20) !== "<title>Sporting News") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === "<link>" && $headlineFound == TRUE) {
				$temp = "";
			}
			else if (substr($data, $i, 7) === "</link>" && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
				$headlines[$j][2] = "";
				$headlines[$j][3] = "Sporting News";
				$headlines[$j][4] = "Sports";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}

/**
 * Parses 5 headlines and their URLs from the Yahoo! Sports RSS feed.
 * 
 * @return array $headlines, a two-dimensional array containing the headline, URL, description, website name, and genre for each headline.
 */
function parse_yahoo_sports_feed() {
	$url = "http://news.yahoo.com/rss/sports";
	$fp = fopen($url, "r");
	$newlineCt = 0; // Counts the number of newline characters found in 
	$headlineCt = 0; // Counts the number of headlines
	$temp = ""; // A temporary variable
	$headline = "";
	$headlineFound = FALSE;
	$link = "";
	$description = "";
	$descriptionFound = FALSE;
	$headlines = array();
	$j = 0;
	
	while ($data = fread($fp,4096)) {
		for ($i = 100; $i < strlen($data); $i++) {
			if (substr($data, $i, 7) === "<title>") {
				if (substr($data, $i, 8) !== "<title>Sports N") {
					$temp = "";
					$headlineFound = TRUE;
				}
			}
			else if (substr($data, $i, 8) === "</title>" && $headlineFound == TRUE) {
				$headline = substr($temp, 6, (strlen($temp) - 6));
				$headlines[$j][0] =	$headline;
				$temp = "";
			}
			else if (substr($data, $i, 6) === 'href="' && $headlineFound == TRUE) {
				$temp = "";
				$linkFound = TRUE;
			}
			else if (substr($data, $i, 5) === '"&gt;' && $headlineFound == TRUE && $linkFound == TRUE) {
				$link = substr($temp, 5, (strlen($temp) - 5));
				$headlines[$j][1] = $link;
				$temp = "";
				$linkFound = FALSE;
			}
			else if (substr($data, $i, 10) === "&lt;/a&gt;" && $headlineFound == TRUE) {
				$temp = "";
				$descriptionFound = TRUE;
			}
			else if (substr($data, $i, 10) === "&lt;/p&gt;" && $descriptionFound == TRUE && $headlineFound == TRUE) {
				$headlineFound = FALSE;
				$descriptionFound = FALSE;
				$description = substr($temp, 9, (strlen($temp)-10));
				$headlines[$j][2] = $description;
				$headlines[$j][3] = "Yahoo! Sports";
				$headlines[$j][4] = "Sports";
				$j++;
				$headlineCt++;
				$temp = "";
				if ($headlineCt == 3) {
					break;
				}
			}
			else {
				$temp = $temp . substr($data, $i, 1);
			}
			if ($headlineCt == 3) {
				break;
			}
		}
		if ($headlineCt == 3) {
			break;
		}
	}
	fclose($fp);
	return $headlines;
}
?>